package com.moxiao.yfjh.module.activity.service.activityenrollverificate;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.activity.controller.admin.activityenrollverificate.vo.ActivityEnrollVerificateCreateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityenrollverificate.vo.ActivityEnrollVerificateExportReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityenrollverificate.vo.ActivityEnrollVerificatePageReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityenrollverificate.vo.ActivityEnrollVerificateUpdateReqVO;
import com.moxiao.yfjh.module.activity.controller.app.vo.AppActivityEnrollVerificatePageReqVO;
import com.moxiao.yfjh.module.activity.controller.app.vo.AppWriteOffVO;
import com.moxiao.yfjh.module.activity.convert.activityenrollverificate.ActivityEnrollVerificateConvert;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityenrollverificate.ActivityEnrollVerificateDO;
import com.moxiao.yfjh.module.activity.dal.mysql.activityenrollverificate.ActivityEnrollVerificateMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.ENROLL_VERIFICATE_NOT_EXISTS;

/**
 * 相亲报名（活动）核销 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class ActivityEnrollVerificateServiceImpl implements ActivityEnrollVerificateService {

    @Resource
    private ActivityEnrollVerificateMapper enrollVerificateMapper;

    @Override
    public Long createEnrollVerificate(ActivityEnrollVerificateCreateReqVO createReqVO) {
        // 插入
        ActivityEnrollVerificateDO enrollVerificate = ActivityEnrollVerificateConvert.INSTANCE.convert(createReqVO);
        enrollVerificateMapper.insert(enrollVerificate);
        // 返回
        return enrollVerificate.getId();
    }

    @Override
    public void updateEnrollVerificate(ActivityEnrollVerificateUpdateReqVO updateReqVO) {
        // 校验存在
        validateEnrollVerificateExists(updateReqVO.getId());
        // 更新
        ActivityEnrollVerificateDO updateObj = ActivityEnrollVerificateConvert.INSTANCE.convert(updateReqVO);
        enrollVerificateMapper.updateById(updateObj);
    }

    @Override
    public void deleteEnrollVerificate(Long id) {
        // 校验存在
        validateEnrollVerificateExists(id);
        // 删除
        enrollVerificateMapper.deleteById(id);
    }

    private void validateEnrollVerificateExists(Long id) {
        if (enrollVerificateMapper.selectById(id) == null) {
            throw exception(ENROLL_VERIFICATE_NOT_EXISTS);
        }
    }

    @Override
    public ActivityEnrollVerificateDO getEnrollVerificate(Long id) {
        return enrollVerificateMapper.selectById(id);
    }

    @Override
    public List<ActivityEnrollVerificateDO> getEnrollVerificateList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return enrollVerificateMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ActivityEnrollVerificateDO> getEnrollVerificatePage(ActivityEnrollVerificatePageReqVO pageReqVO) {
        return enrollVerificateMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ActivityEnrollVerificateDO> getEnrollVerificateList(ActivityEnrollVerificateExportReqVO exportReqVO) {
        return enrollVerificateMapper.selectList(exportReqVO);
    }


    @Override
    public BigDecimal getPrice(Long activityId, Long userId, Byte status, Long enrollId) {
        return enrollVerificateMapper.getPrice(activityId, userId, status, enrollId);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBatch(Long enrollId, Long userId, Byte type) {
        ActivityEnrollVerificateExportReqVO query = new ActivityEnrollVerificateExportReqVO();
        query.setEnrollId(String.valueOf(enrollId));
        query.setUserId(userId);
        List<ActivityEnrollVerificateDO> list = enrollVerificateMapper.selectList(query);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                ActivityEnrollVerificateUpdateReqVO update = new ActivityEnrollVerificateUpdateReqVO();
                update.setId(item.getId());
                update.setType(type);
                updateEnrollVerificate(update);
            });
        }

    }


    @Override
    public int updateStatus(Long id) {
        LocalDateTime now = LocalDateTime.now();
        // 修改核销状态
        int t = enrollVerificateMapper.update(null, new LambdaUpdateWrapper<ActivityEnrollVerificateDO>()
                .set(ActivityEnrollVerificateDO::getStatus, 1)
                .set(ActivityEnrollVerificateDO::getVerificationTime, now)
                .eq(ActivityEnrollVerificateDO::getId, id));
        return t;
    }

    @Override
    public PageResult<AppWriteOffVO> getMerchantVerificationList(AppActivityEnrollVerificatePageReqVO reqVO) {
        return enrollVerificateMapper.getMerchantVerificationList(reqVO);
    }
}
